/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.dictionary;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.entity.Dictionary;
import org.grayrabbit.cms.entity.DictionaryType;
import org.grayrabbit.cms.service.dictionary.DictionaryService;
import org.grayrabbit.cms.service.dictionary.DictionaryTypeService;
import org.grayrabbit.cms.vo.dictionary.DictionaryTypeBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>DictionaryTypeAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 14:46:53 
 */
@Controller
@Scope("prototype")
@Results( {
		@Result(name = "reload", type = "redirectAction", location = "dictionarytype-list"),
		@Result(name = "input", type = "dispatcher", location = "dictionarytype-input.jsp"),
		@Result(name = "success", type = "dispatcher", location = "dictionarytype-list.jsp")
		}
		)
public class DictionaryTypeAction extends GenericAction<DictionaryTypeBean> {
	private static final long serialVersionUID = -1L;
	
	private static final String IS_EXIST = "1";
	private static final String IS_NOT_EXIST = "0";
	//Service接口接入
	@Autowired
	@Qualifier("dictionaryTypeServiceImpl")
	private DictionaryTypeService dictionaryTypeService;
	
	@Autowired
	@Qualifier("dictionaryServiceImpl")
	private DictionaryService dictionaryService;
	
	
	//VO对象，由前端页面数据组装
	private DictionaryTypeBean dictionaryType;
	
	/**return 前端页面数据组装VO对象,<b>DictionaryTypeBean</b>*/
	public DictionaryTypeBean getDictionaryType() {
		return dictionaryType;
	}

	public void setDictionaryType(DictionaryTypeBean dictionaryType) {
		this.dictionaryType = dictionaryType;
	}
	
	private List<Dictionary> dicIsEnable;
	private List<Dictionary> dicIsModify;
	private List<Dictionary> dictionarys;
	private String isEnable;
	
	

	public List<Dictionary> getDictionarys() {
		return dictionarys;
	}

	public void setDictionarys(List<Dictionary> dictionarys) {
		this.dictionarys = dictionarys;
	}

	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	public List<Dictionary> getDicIsModify() {
		return dicIsModify;
	}

	public void setDicIsModify(List<Dictionary> dicIsModify) {
		this.dicIsModify = dicIsModify;
	}

	public List<Dictionary> getDicIsEnable() {
		return dicIsEnable;
	}

	public void setDicIsEnable(List<Dictionary> dicIsEnable) {
		this.dicIsEnable = dicIsEnable;
	}

	//分页对象
	private Page<DictionaryType> page = new Page<DictionaryType>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<DictionaryType> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<DictionaryType> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_ENABLE)!=null){
			dicIsEnable = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_ENABLE);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_MODIFY)!=null){
			dicIsModify = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_MODIFY);
		}
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(dictionaryType==null){
			dictionaryType = new DictionaryTypeBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public DictionaryTypeBean getModel() {
		return dictionaryType;
	}
	
	@Override
	protected void prepareModel(){
		if(dictionaryType!=null&&StringUtils.isNotBlank(dictionaryType.getDicTypeId())){
			dictionaryType = (DictionaryTypeBean)dozer.map(dictionaryTypeService.getDictionaryType(dictionaryType.getDicTypeId()),DictionaryTypeBean.class);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_ENABLE)!=null){
			dicIsEnable = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_ENABLE);
		}
		if(dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_MODIFY)!=null){
			dicIsModify = dictionaryService.getDictionaryListByTypeCode(DictionaryStaticValues.IS_MODIFY);
		}
	}

	/**
	 * 删除<b>DictionaryType</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到dictionarytype-list.action
	 */
	@Override
	@Action(value="dictionarytype-delete")
	public String delete(){
		dictionaryTypeService.delete(key);
		return RELOAD;
	}
	
	/**
	 * DictionaryType的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到dictionarytype-list.jsp页面
	 */
	@Override
	@Action(value="dictionarytype-list")
	public String list(){
		if(StringUtils.isNotBlank(isEnable)){
			if(DictionaryStaticValues.IS_ENABLE_TRUE.equals(isEnable)){
				dictionaryType.setIsEnable(true);
			}else{
				dictionaryType.setIsEnable(false);
			}
		}
		dictionaryTypeService.list(page,dictionaryType);
		return SUCCESS;
	}
	/**
	 * DictionaryType的新增与编辑的方法执行成功能
	 * 将映射到dictionarytype-input.jsp
	 */
	@Action(value="dictionarytype-input")
	public String input(){
		prepareModel();
		if(dictionaryType!=null&&StringUtils.isNotBlank(dictionaryType.getDicTypeCode())){
			dictionarys = dictionaryService.getParentDictionaryList(dictionaryType.getDicTypeCode());
		}
		return INPUT;
	}
	
	/**
	 * save or update DictionaryType的方法，将成员property dictionaryType转成DictionaryType实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到dictionarytype-list.action
	 */
	@Override
	@Action(value="dictionarytype-save")
	public String save(){
		DictionaryType obj = (DictionaryType) dozer.map(dictionaryType, DictionaryType.class);
		if(!StringUtils.isNotBlank(obj.getDicTypeId())){
			obj.setDicTypeId(null);
		}else{
			if(StringUtils.isNotBlank(dictionaryType.getDicDelCode())){
				dictionaryService.deleteBatch(dictionaryType.getDicDelCode());
			}
		}
		dictionaryTypeService.save(obj);
		if(StringUtils.isNotBlank(dictionaryType.getDicCode())){
			dictionaryService.saveBatch(dictionaryType.getDicCode(),obj);
		}
		return RELOAD;
	}
	
	/**
	 * 获取数据字典KEY值对应的NAME
	 * 
	 * @param key
	 * @param type
	 *            数据字典类型
	 * @return
	 */
	public String getName(String key,String code) {
		return dictionaryService.getDictionaryByDictionaryKey(key,code).getDictionaryValue();
	}
	/**
	 * 异步验证字典CODE是否存在
	 * @return
	 */
	@Action(value="dictionarytype-validate")
	public String validateDicTypeCode(){
		List<Dictionary> list = dictionaryService.getDictionaryListByTypeCode(dictionaryType.getDicTypeCode());
		if(list!=null){
			if(list.isEmpty()){
				return renderText(IS_NOT_EXIST);
			}else{
				return renderText(IS_EXIST);
			}
		}else{
			return renderText(IS_NOT_EXIST);
		}
	}
	@Action(value="dictionarytype-child")
	public String getDictionaryChild(){
		String info = "[{";
		if(dictionaryType!=null&&StringUtils.isNotBlank(dictionaryType.getDicTypeId())){
			dictionarys = dictionaryService.getAllDictionaryListByParentId(dictionaryType.getDicTypeId());
			for(Dictionary dic:dictionarys){
				info+="dictionaryId:'"+dic.getDictionaryId()+"',";
				info+="dictionaryKey:'"+dic.getDictionaryKey()+"',";
				info+="dictionaryValue:'"+dic.getDictionaryValue()+"',";
				info+="memo:'"+dic.getMemo()+"',";
				info+="ord:'"+dic.getOrd()+"',";
				info+="isLeaf:'"+dic.getIsLeaf()+"',";
				info+="parentId:'"+dic.getDictionary().getDictionaryId()+"'},{";
			}
		}
		info=info.substring(0, info.length()-2);
		info+="]";
		return renderText(info);
	}
	
	public String getMemo(String memo){
		String text;
		if((StringUtils.isNotBlank(memo))&&(memo.length()>12)){
			text=memo.substring(0,10)+"...";
		}else{
			text=memo;
		}
		return text;
	}
}